###############################################
# 函数的定义
from typing import Optional
def int_add(x: int, y: int) -> Optional[int]:
    """
    实现整数加法，对浮点型数据只取整数部分参与运算
    """
    try:
        _x = int(x)
        _y = int(y)
    except Exception:
        print('请输入整型参数，float类型将被截断！')
        return None
    return _x + _y
r1 = int_add(1, 3.9+2j)
print(r1)
r1 = int_add(1, 3.9)
print(r1)
r2 = int_add(**{'x':1, 'y':3.9})
print(r1 == r2)

def show_args(*args, **kwargs):
    """ 变长参数和具名参数示例 """
    print(args, kwargs)

show_args(1, '2s', x='2', y='6')
show_args(x='2', y='6')
show_args(1, 'a', **{'name':'wang', 'age':3})

def add(*args):
    return sum(args)


print(add(1, 2))
print(add(1, 2, 3))

# add(1,'2')


def add2(*args):
    try:
        return sum(args)
    except TypeError as e:
        print('输入类型错误，详情：', e)
        print('请核实后输入')
        return None


print(add2(1, 2))
print(add2(1, 2, 3.0))
print(add2(1, '2'))


def calculate(*args, opr='sum'):
    result = None
    if len(args) > 0:
        result = args[0]
        try:
            if opr == 'sum':
                result = sum(args)
            elif opr == 'sub':
                for i in range(1,len(args)):
                    result -= args[i]
            elif opr == 'mul':
                for i in range(1, len(args)):
                    result *= args[i]
            elif opr == 'div':
                if 0 in args[1:]:
                    raise ZeroDivisionError('除数不可为0')
                i = 1
                while i < len(args):
                    result /= args[i]
                    i += 1
        except TypeError as e:
            print('参与运算的参数类型错误，请核查！')
            print('错误详情：', e)
    return result

print(calculate(1,2,3))
print(calculate(1, 2, 3, opr='sub'))
print(calculate(1, 2, 3, opr='mul'))
print(calculate(1, 2, 3, opr='div'))

args = (1,2,'3')
kwargs = {'opr': 'sub'}
print(calculate(*args))
print(calculate(*args, **kwargs))
